package ru.spies.content;

import java.util.Collection;

import ru.spies.dbwork.DBQueries;
import ru.spies.dbwork.DBWork;
import ru.spies.entities.Agent;
import ru.spies.entities.Country;
import ru.spies.entities.Department;


public class LoginOrRegistrationContent extends AbstractContent{
	
	private static LoginOrRegistrationContent statThis=null;
	
	private static final String ALL_AGENTS_WAS_REGISTERED="<p>Все агенты были зарегистрированны!!!</p>"+CONTENT_ERROR;

	private LoginOrRegistrationContent() {
		
	}
	
	public static LoginOrRegistrationContent getInstance(){
		if (statThis==null){
			statThis=new LoginOrRegistrationContent();
		}
		return statThis;
	}
	
	public String getLoginContent(){
		/*
		 *  <p>Логин:</p>
			<input type="text" class="txtBoxLoginForm" name="loginField">
			<p>Пароль:</p>
			<input type="password" class="txtBoxLoginForm" name="passwordField">
			<br>
			<input type="submit" class="inFormBtnLoginForm" value="Вход">
			<input type="button" onclick="goRegister()" value="Регистрация" class="inFormBtnLoginForm">
		 */
		return "<p>Логин:</p><input type=\"text\" class=\"txtBoxLoginForm\" name=\"loginField\"><p>Пароль:</p><input type=\"password\" class=\"txtBoxLoginForm\" name=\"passwordField\"><br><input type=\"submit\" class=\"inFormBtnLoginForm\" value=\"Вход\" onclick=\"Enter()\"><input type=\"button\" onclick=\"goRegister()\" value=\"Регистрация\" class=\"inFormBtnLoginForm\">";
	}
	
	public String getRegistrationContent(){
		/*
		 * <p>Логин:</p>
			<input type="text" class="txtBoxLoginForm" name="loginField">
				checkbox
			<p>Пароль:</p>
			<input type="password" class="txtBoxLoginForm" name="passwordField" onkeyup="checkPasswordForASCII()">
			<br><span name="ASCIIResult" class="msg"></span>
			<p>Подтвердите пароль:</p>
			<input type="password" class="txtBoxLoginForm" name="confirmField" onkeyup="checkConfirmed()">
			<br><span name="checkResult" class="msg"></span>
			<p>Ваше имя:</p>
			<select class="txtBoxLoginForm" name="agentName">
				options
			</select>
			<p>Ваш отдел:</p>
			<select class="txtBoxLoginForm" name="departmentName">
				options
			</select>
			<p>Ваша страна:</p>
			<select class="txtBoxLoginForm" name="countryName">
				options
			</select>
			<input type="submit" class="inFormBtn" value="Регистрация" onclick="doRegistration()">
		 */
		StringBuffer htmlContent = new StringBuffer("<p>Логин:</p><input type=\"text\" class=\"txtBoxLoginForm\" name=\"loginField\">"+askForPasswordGeneretor()+"<p>Пароль:</p><input type=\"password\" class=\"txtBoxLoginForm\" name=\"passwordField\" onkeyup=\"checkPasswordForASCII()\"><br><span name=\"ASCIIResult\" class=\"msg\"></span><p>Подтвердите пароль:</p><input type=\"password\" class=\"txtBoxLoginForm\" name=\"confirmField\" onkeyup=\"checkConfirmed()\"><br><span name=\"checkResult\" class=\"msg\"></span><p>Ваше имя:</p><select class=\"txtBoxLoginForm\" name=\"agentName\">");
		Collection<Agent> agents = (Collection<Agent>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_AGENT_FIND);
		boolean hasOneOrMoreAgents=false;
		if (agents.size()!=0){
			for (Agent agent : agents) {
				if (!agent.isRegistered()){
					htmlContent.append(getComboOption(agent.getName(),agent.getId().toString()));
					hasOneOrMoreAgents=true;
				}
			}
		} else {
			return AGENT_ERROR;
		}
		if (!hasOneOrMoreAgents) return ALL_AGENTS_WAS_REGISTERED;
		htmlContent.append("</select><p>Ваш отдел:</p><select class=\"txtBoxLoginForm\" name=\"departmentName\">");
		Collection<Department> departments = (Collection<Department>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_DEPARTMENT_FIND);
		if (departments.size()!=0){
			for (Department department : departments) {
				htmlContent.append(getComboOption(department.getName(),department.getId().toString()));
			}
		} else {
			return DEPARTMENT_ERROR;
		}
		htmlContent.append("</select><p>Ваша страна: </p><select class=\"txtBoxLoginForm\" name=\"countryName\">");
		Collection<Country> countries = (Collection<Country>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_COUNTRY_FIND);
		if (countries.size()!=0){
			for (Country country : countries) {
				htmlContent.append(getComboOption(country.getName(),country.getId().toString()));
			}
		} else {
			return COUNTRY_ERROR;
		}
		htmlContent.append("</select><input type=\"submit\" class=\"inFormBtn\" value=\"Регистрация\" onclick=\"doRegistration()\">");
		return htmlContent.toString();
	}
	
	public String getSuccessfulContent(){
		/*
		 * <p>Действие успешно завершено.</p>
			<input type="button" value="На главную" onclick="goHome()" class="inFormBtn">
		 */
		return "<p>Действие успешно завершено.</p><input type=\"button\" value=\"На главную\" onclick=\"goHome()\" class=\"inFormBtn\">";
	}
	
	public String getFailureContent(String errorMessage){
		/*
		 * <p>errorMessage</p>
			<input type="button" value="На главную" onclick="goHome()">
		 */
		return errorMessage+"<input class=\"inFormBtn\" type=\"button\" value=\"На главную\" onclick=\"goHome()\">";
	}
	
	public String getExitContent(){
		return "<p>Вы уже вошли на сайт!</p><p>Если вы хотетите перезайти, то сперва покиньте сайт</p><span name=\"loginField\"></span><input class=\"inFormBtn\" value=\"Выход\" type=\"submit\" onclick=\"Exit()\">";
	}
	
	public String getAfterRegistrationContent(String userName, String password){		
		return "<p>Успешная регистрация пользователя:</p><p>"+userName+"</p><p>Ваш пароль:</p><p>"+password+"</p>"+RETURN_HOME;
	}

}
